package com.ruoyi.business.ajax;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.github.pagehelper.PageHelper;
import com.ruoyi.business.ajax.request.BasicRequest;
import com.ruoyi.business.domain.BizProduct;
import com.ruoyi.business.domain.BizProductType;
import com.ruoyi.business.mapper.BizAccountMapper;
import com.ruoyi.business.mapper.BizMemberMapper;
import com.ruoyi.business.service.IBizProductService;
import com.ruoyi.business.service.IBizProductTypeService;
import com.ruoyi.business.sync.UserData;
import com.ruoyi.business.sync.UserDataListener;
import com.ruoyi.common.annotation.AjaxLogin;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@AjaxLogin
@RestController
@RequestMapping("/ajax/product")
public class AjaxProductController extends AuthController {

    @Resource
    private IBizProductService bizProductService;

    @Resource
    private IBizProductTypeService bizProductTypeService;

    //分类和推荐商品
    @PostMapping("/center")
    public AjaxResult center()
    {
        //首页分类
        Map<String, Object> resultMap = new HashMap<String, Object>();
        BizProductType productType = new BizProductType();
        productType.setIsEnable(0);
        resultMap.put("typeList", bizProductTypeService.selectBizProductTypeList(productType));

        //首页商品
        PageHelper.startPage(1, 8);
        resultMap.put("productList", bizProductService.selectOnlineProductList());
        return AjaxResult.success(resultMap);
    }

    //产品分类及列表
    @PostMapping("/list")
    public AjaxResult list()
    {
        BizProductType productType = new BizProductType();
        productType.setIsEnable(0);
        List<BizProductType> list = bizProductTypeService.selectBizProductTypeList(productType);
        List<Map> typeList = new ArrayList<>();
        Map temp = new HashMap();
        for (BizProductType type : list) {
            Map item = new HashMap();
            item.put("id", type.getId());
            item.put("name", type.getProductTypeName());
            item.put("products", new ArrayList<>());
            typeList.add(item);
            temp.put(type.getId(), item);
        }
        List<Map> productList = bizProductService.selectOnlineProductList();
        for (Map product : productList) {
            Long typeId = (Long) product.get("typeId");
            Map type = (Map) temp.get(typeId);
            if (type != null) {
                ((List) type.get("products")).add(product);
            }
        }
        return AjaxResult.success(typeList);
    }

    //读取产品
    @PostMapping("/load")
    public AjaxResult load(@RequestBody BasicRequest request)
    {
        BizProduct bizProduct = bizProductService.selectBizProductById(request.getId());
        return AjaxResult.success(bizProduct);
    }

    //简易商品列表
    private List<Map> getSimpleProductList(List<BizProduct> productList)
    {
        List<Map> resultList = new ArrayList<Map>();
        for (BizProduct bizProduct : productList) {
            Map map = new HashMap();
            map.put("id", bizProduct.getId());
            map.put("typeID", bizProduct.getProductTypeId());
            map.put("name", bizProduct.getProductName());
            map.put("mainImage", bizProduct.getMainImage());
            map.put("price", bizProduct.getAmount());
            resultList.add(map);
        }
        return resultList;
    }
}
